home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lib.s5 / ttyraw.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  56 lines

  1. /*
  2.  * Put a terminal device into RAW mode with ECHO off.
  3.  * Before doing so we first save the terminal's current mode,
  4.  * assuming the caller will call the tty_reset() function
  5.  * (also in this file) when it's done with raw mode.
  6.  */
  7.  
  8. #include    <termio.h>
  9.  
  10. static struct termio    tty_mode;    /* save tty mode here */
  11.  
  12. int
  13. tty_raw(fd)
  14. int    fd;        /* of terminal device */
  15. {
  16.     struct termio    temp_mode;
  17.  
  18.     if (ioctl(fd, TCGETA, (char *) &temp_mode) < 0)
  19.         return(-1);
  20.     tty_mode = temp_mode;        /* save for restoring later */
  21.  
  22.     temp_mode.c_iflag  = 0;        /* turn off all input control */
  23.     temp_mode.c_oflag &= ~OPOST;    /* disable output post-processing */
  24.     temp_mode.c_lflag &= ~(ISIG | ICANON | ECHO | XCASE);
  25.                     /* disable signal generation */
  26.                      /* disable canonical input */
  27.                     /* disable echo */
  28.                     /* disable upper/lower output */
  29.     temp_mode.c_cflag &= ~(CSIZE | PARENB);
  30.                     /* clear char size, disable parity */
  31.     temp_mode.c_cflag |= CS8;    /* 8-bit chars */
  32.  
  33.     temp_mode.c_cc[VMIN]  = 1;    /* min #chars to satisfy read */
  34.     temp_mode.c_cc[VTIME] = 1;    /* 10'ths of seconds between chars */
  35.  
  36.     if (ioctl(fd, TCSETA, (char *) &temp_mode) < 0)
  37.         return(-1);
  38.  
  39.     return(0);
  40. }
  41.  
  42. /*
  43.  * Restore a terminal's mode to whatever it was on the most
  44.  * recent call to the tty_raw() function above.
  45.  */
  46.  
  47. int
  48. tty_reset(fd)
  49. int    fd;        /* of terminal device */
  50. {
  51.     if (ioctl(fd, TCSETA, (char *) &tty_mode) < 0)
  52.         return(-1);
  53.  
  54.     return(0);
  55. }
  56.